//////////////////////////////////////////////////////////////////////////
///	\file:	IView.h
///	Copyright (MingYou) 2009 - All Rights Reserved
///	Author:	Robert.Ma
///	Date:	[2009/06/22]
///	Description:
///
//////////////////////////////////////////////////////////////////////////
#pragma once
#include "ViewCommand.h"
#include "Define.h"


typedef struct tagViewCmd
{
	UINT	uiID;
	WPARAM	wParam;
	LPARAM	lParam;
	DWORD	dwData;
} VIEWCMD;

struct IView
{
	virtual LRESULT HandleCommand(const VIEWCMD& cmd) PURE;
	virtual LRESULT Create(LPCRECT lpRc, LPVOID lpParam1, LPVOID lpParam2) PURE;
};

//////////////////////////////////////////////////////////////////////////
//	Macros for message.
#define BEGIN_HANDLE_COMMAND(className) \
LRESULT className::HandleCommand(const VIEWCMD& vcCmd) \
{\
	LRESULT lResult = L_ZERO; \
	switch ( vcCmd.uiID ) \
	{

//	memberFunc should like "LRESULT HandleCmd***(const VIEWCMD& vcCmd)".
#define HANDLE_COMMAND(message, memberFunc) \
	case message:\
		lResult =  memberFunc(vcCmd);\
	break;

#define END_HANDLE \
	default:\
		break;\
	}\
	return lResult;\
}

#define EMPTY_HANDLE_COMMAND(className) \
LRESULT className::HandleCommand(const VIEWCMD&) \
{\
	return L_ZERO;\
}

#define DECLARE_HANDLE_COMMAND \
virtual LRESULT HandleCommand(const VIEWCMD& vcCmd);

LRESULT inline SendCommand(IView*	pWindow,
						   UINT		uiID,
						   WPARAM	wParam	= 0,
						   LPARAM	lParam	= 0,
						   DWORD	dwData	= 0)
{
	LRESULT lResult = L_ZERO;

	if ( NULL != pWindow )
	{
		VIEWCMD vcCmd = {uiID, wParam, lParam, dwData};

		lResult = pWindow->HandleCommand(vcCmd);
	}

	return lResult;
}

struct IMemoryGrapics;

LRESULT inline DrawView(IView* pView, IMemoryGrapics* pMG)
{
	return ::SendCommand(pView, VM_DRAW, 0, 0, (DWORD)(pMG));
}

LRESULT inline HandleLBtnDown(IView* pView, WORD wXpos, WORD wYpos)
{
	DWORD wParam = MAKEWPARAM(wXpos, wYpos);

	return ::SendCommand(pView, VM_LBUTTON_DOWN, wParam);
}

LRESULT inline HandleLBtnUp(IView* pView, WORD wXpos, WORD wYpos)
{
	DWORD wParam = MAKEWPARAM(wXpos, wYpos);

	return ::SendCommand(pView, VM_LBUTTON_UP, wParam);
}

LRESULT inline HandleRBtnDown(IView* pView, WORD wXpos, WORD wYpos)
{
	DWORD wParam = MAKEWPARAM(wXpos, wYpos);

	return ::SendCommand(pView, VM_RBUTTON_DOWN, wParam);
}

LRESULT inline HandleRBtnUp(IView* pView, WORD wXpos, WORD wYpos)
{
	DWORD wParam = MAKEWPARAM(wXpos, wYpos);

	return ::SendCommand(pView, VM_RBUTTON_UP, wParam);
}
